package com.aranandroid.customview.text

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import androidx.appcompat.widget.AppCompatTextView
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener

class TouchTextView : AppCompatTextView {
    constructor(context: Context?) : super(context!!) {
        setOnTouchListener()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(
        context!!, attrs
    ) {
        setOnTouchListener()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!, attrs, defStyleAttr
    ) {
        setOnTouchListener()
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return super.onTouchEvent(event)
    }

    fun setOnTouchListener() {
        val onTouchListener: OnTouchListener = object : OnTouchListener {
            private var startY = 0
            private var startX = 0
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        Log.e("打印操作：", "按下了")
                        //获取当前按下的坐标
                        startX = event.rawX.toInt()
                        startY = event.rawY.toInt()
                    }
                    MotionEvent.ACTION_MOVE -> {
                        //获取移动后的坐标
                        val moveX = event.rawX.toInt()
                        val moveY = event.rawY.toInt()
                        //拿到手指移动距离的大小
                        val move_bigX = moveX - startX
                        val move_bigY = moveY - startY
                        Log.e("打印操作：", "\nX移动了$move_bigX\nY移动了$move_bigY")
                        //拿到当前控件未移动的坐标
                        var left = v.left
                        var top = v.top
                        left += move_bigX
                        top += move_bigY
                        if (left > 0) {
                            left = 0
                        }
                        if (left < -width) {
                            left = -width
                        }
                        if (top > 0) {
                            top = 0
                        }
                        if (top < -height) {
                            top = -height
                        }
                        val right = left + v.width
                        val bottom = top + v.height
                        v.layout(left, top, right, bottom)
                        startX = moveX
                        startY = moveY
                    }
                    MotionEvent.ACTION_UP -> Log.e("打印操作：", "抬起了")
                }
                return true //此处一定要返回true，否则监听不生效
            }
        }
        super.setOnTouchListener(onTouchListener)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
    }
}